package com.huluspace.nosqlredis.controller;

import com.huluspace.nosqlredis.dto.UserDTO;
import com.huluspace.nosqlredis.security.JwtUtil;
import com.huluspace.nosqlredis.service.JwtAuthService;
import com.huluspace.nosqlredis.service.UserService;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class UserController {
  // 注入
  private final UserService userService;
  public UserController(UserService userService) {
    this.userService = userService;
  }

  // 用户列表
  @GetMapping("/list")
  public List<UserDTO> users() {
    return userService.listAllUsers();
  }

  // 用户信息
  @GetMapping("/me")
  public ResponseEntity<?> getUserInfo(@AuthenticationPrincipal UserDetails user) {
    // 或者从SecurityContextHolder获取
    return ResponseEntity.ok(Map.of(
        "username", user.getUsername(),
        "email", "无"  // 示例数据
    ));
  }

  // 去激活某个用户（如禁止登录）
  @PostMapping("/deactivate")
  public ResponseEntity<String> deactivateUser(@RequestParam String username) {
    userService.deactivateUser(username);
    return ResponseEntity.ok("用户已去激活");
  }

}
